const express = require('express');
const router = express.Router();

router.get('/:name/:child', async (req, res, next) => {
	try {
		const app = await query("SELECT * FROM mgtv.routes where name='app*'");
		const project = await query("SELECT * FROM mgtv.routes where name='project*'");
		// const agent = await query('SELECT * FROM sure.goods where name=\'agent\'');
		// const coat = await query('SELECT * FROM sure.goods where name=\'coat\'');
		// const jacket = await query('SELECT * FROM sure.goods where name=\'jacket\'');
		// const trousers = await query('SELECT * FROM sure.goods where name=\'trousers\'');
		// const child = await query('SELECT * FROM sure.goods where name=\'child\'');

		if (req.params.name == 'app*') {
			res.render('page/table/app/' + req.params.name, {
				title: getTitle(res, req.params.name),app
			});
		} else if (req.params.name == 'project*') {
			res.render('page/table/project/' + req.params.name, {
				title: getTitle(res, req.params.name),project
			});
		} else {
			res.render('page/table/app/' + req.params.name, {
				title: getTitle(res, req.params.name),
			});
		}
	} catch (err) {
		next(err);
	}
});

function getTitle(res, name) {
	for (const c of res.locals.uname) {
		if (c.name.indexOf(name) !== -1) {
			return c.type;
		}
	}
	return '';
}
module.exports = router;
